﻿using System;
using Evpo.ContactData.ViewModel;
using System.Collections.Specialized;
using System.Web;
using Evpo.ContactData.Common;
using Evpo.ContactData.UI.GeneralClasses;

namespace Evpo.ContactData.UI
{
    partial class ContactList
    {
        protected class UrlComposer : ListUrlComposer
        {
            public UrlComposer(ContactListViewModel model, Uri baseUrl)
                : base(baseUrl)
            {
                _model = model;
            }

            #region Fields
            private readonly ContactListViewModel _model;

            #endregion

            #region Properties
            protected override ListViewModel ListViewModel
            {
                get { return _model; }
            }

            #endregion

            #region Methods

            public Uri UrlForSortField(ContactSortFields sortField)
            {
                var builder = new UriBuilder(CurrentUrl);

                ContactQueryParameters pmt =
                    new ContactQueryParameters(_model.ContactQuery);

                pmt.SortField = sortField;

                builder.Query = CreateQueryCollection(builder.Query, pmt).ToString();

                return builder.Uri;
            }

            public Uri ContactItemUrl(int contactID)
            {
                return UrlHelper.ContactUrl(contactID, CurrentUrl);
            }

            protected override void UpdateParametersInNameValueCollection(NameValueCollection collection, ListQueryParameters listParameters)
            {
                base.UpdateParametersInNameValueCollection(collection, listParameters);

                ContactQueryParameters parameters = (ContactQueryParameters)listParameters;

                UpdateParameterInCollection(collection, QueryKeys.SortField, parameters.SortField.ToString());
                UpdateParameterInCollection(collection, QueryKeys.FirstNameContains, parameters.FirstNameContains);
                UpdateParameterInCollection(collection, QueryKeys.LastNameContains, parameters.LastNameContains);
            }
            
            internal static ContactQueryParameters ExtractQueryParameters(NameValueCollection query)
            {
                var retVal = new ContactQueryParameters()
                {
                    SortField = ContactSortFields.Default,
                    PageBegin = 1,
                    PageEnd = Preferences.PageCount,
                };

                string paramValue = query[QueryKeys.SortField];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.SortField = (ContactSortFields)Enum.Parse(typeof(ContactSortFields), paramValue);

                paramValue = query[QueryKeys.PageBegin];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.PageBegin = Int32.Parse(paramValue);

                paramValue = query[QueryKeys.PageEnd];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.PageEnd = Int32.Parse(paramValue);

                paramValue = query[QueryKeys.FirstNameContains];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.FirstNameContains = paramValue;

                paramValue = query[QueryKeys.LastNameContains];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.LastNameContains = paramValue;

                return retVal;
            }

            #endregion Methods
        }
    }
}
